/*
Filename: UCDeleteMgr.java
Precondition: manager enter a valid manager name and password
Post-condition: manager can delete from the system.
Main success Scenario:
1 Manager selects to delete
2. Manager enters username and password
3. Manager clicks ‘delete’ button
4. System retrieves manager name and password
5. System verifies the delete manager information
6. System deletes the manager information
7. System deletes manager from the list of online manager
8. System displays message “manager Deleted”
System updates the main view.
2a. Extensions: System detects invalid delete:
System display “invalid manager delete” message

 */

package ucm;

/**
 *
 * @author Mark
 */
import model.UsersModel;
import model.DatabaseModel;
import view.deleteMgr;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class UCDeleteMgr implements UCController {

    private UsersModel user;

    public UCDeleteMgr() {
        user = new UsersModel("", "", "", "", "");
    }

    public UCDeleteMgr(UsersModel user) {
        // System retrieves username and password
        this.user = user;
    }

    public UCDeleteMgr(deleteMgr deleteMgr) {
        // System retrieves username and password
        this.user = deleteMgr.getUserModel();
    }

    // STEP 1 - User Selects Delete
    public boolean run() {
        boolean deleteSuccessful = false;
        // STEP 2 - System retrieves username and password
        // This step is integrated into the constructor or
        // may be specified by using run(UserModel)
        // STEP 3 - System validates the username and password
        if (validUser()) {
            //Step 4 - System deletes user from the database
            if (deleteUser()) {
                //Step 5 - System displays 'user deleted' message
                JOptionPane.showMessageDialog(new JFrame(),
                        "Manager Deleted Successfully",
                        "Success",
                        JOptionPane.INFORMATION_MESSAGE);
                deleteSuccessful = true;
                new UCDispalyTicketsaler();
            } else {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Deletion failed for unknown reason",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                new UCDisplayDeleteMgr();
            }
        } else {
            // STEP 2a - System displays error message
            JOptionPane.showMessageDialog(new JFrame(),
                    "Invalid Username or Password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
                   new UCDisplayDeleteMgr();
        }
        return deleteSuccessful;
    }

    // STEP 1 - User Selects Delete
    public void run(UsersModel user) {
        // STEP 2 - System retrieves username and password
        this.user = user;
        // STEP 3 onwards
        run();
    }

    private boolean validUser() {
        boolean correct = false;
        String username = user.getUsername();
        String password = user.getPassword();

        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet res = stmt.executeQuery("SELECT * FROM managers WHERE " +
                    "mname = '" + username + "'");

            correct = res.first() && username.equals(res.getString("mname"))
                    && password.equals(res.getString("mpassword"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return correct;
    }

    private boolean deleteUser() {
        boolean delete = false;
        String username = user.getUsername();
        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate("DELETE FROM managers WHERE mname = '"
                    + username + "'");

            ResultSet res = stmt.executeQuery("SELECT * FROM managers " +
                    "WHERE mname = '" + username + "'");

            delete = !(res.first() && username.equals(res.getString("mname")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete;
    }
}


